package com.captain.leetcode.动态规划;

import java.util.Arrays;

/**
 * @author captain
 * @Des
 * @Date 2021/7/25 19:31
 */
public class 不同路径II63 {
    //    [][][]
//    [][][1,2]  [m-2,n-1]
//    [][][1,2]  [m-1,n-2]
//    [][2,1][2,2]n
    public static void main(String[] args) {
        //[[0,0],[1,1],[0,0]]
        //System.out.println(new 不同路径II63().uniquePathsWithObstacles1(dp));
    }

    public int uniquePathsWithObstacles(int[][] obstacleGrid) {
        //m 行,n 列
        int m = obstacleGrid.length;
        int n = obstacleGrid[0].length;
        int[][] dp = new int[m][n];
        for (int i = 0; i < m && (obstacleGrid[i][0] == 0); i++) dp[i][0] = 1;
        for (int i = 0; i < n && (obstacleGrid[0][i] == 0); i++) dp[0][i] = 1;
        for (int i = 1; i < m; i++) {
            for (int j = 1; j < n; j++) {
                if (obstacleGrid[i][j] == 1) continue;
                dp[i][j] = dp[i - 1][j] + dp[i][j - 1];
            }
        }
        return dp[m - 1][n - 1];

    }

    // [0][0]
//    [1][1]
//    [0][0]
    public int uniquePathsWithObstacles1(int[][] obstacleGrid) {
        //m 行,n 列
        int m = obstacleGrid.length;
        int n = obstacleGrid[0].length;

        int[][] dp = new int[m][n];

        for (int i = 0; i < m && obstacleGrid[i][0] == 0; i++) {
            dp[i][0] = 1;
        }

        for (int i = 0; i < n; i++) {
            if (obstacleGrid[0][i] == 0) {
                dp[0][i] = 1;
            } else {
                break;
            }
        }

        for (int i = 1; i < m; i++) {
            for (int j = 1; j < n; j++) {
                if (obstacleGrid[i][j] == 1) continue;
                dp[i][j] = dp[i - 1][j] + dp[i][j - 1];
            }
        }
        return dp[m - 1][n - 1];
    }
}
